{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# performance\n",
    "\n",
    "## 介绍\n",
    "因子选股研究中常用的绩效计算方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import warnings\n",
    "warnings.filterwarnings('ignore')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dataview loaded successfully.\n",
      "Nan Data Count (should be zero) : 0;  Percentage of effective data: 99%\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>signal</th>\n",
       "      <th>return</th>\n",
       "      <th>group</th>\n",
       "      <th>quantile</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th>symbol</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">20170503</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <td>6.7925</td>\n",
       "      <td>-0.015258</td>\n",
       "      <td>480000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000002.SZ</th>\n",
       "      <td>10.0821</td>\n",
       "      <td>0.013463</td>\n",
       "      <td>430000</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000008.SZ</th>\n",
       "      <td>42.9544</td>\n",
       "      <td>-0.122721</td>\n",
       "      <td>640000</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000009.SZ</th>\n",
       "      <td>79.4778</td>\n",
       "      <td>-0.155903</td>\n",
       "      <td>510000</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>000027.SZ</th>\n",
       "      <td>20.4542</td>\n",
       "      <td>-0.041935</td>\n",
       "      <td>410000</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                       signal    return   group  quantile\n",
       "trade_date symbol                                        \n",
       "20170503   000001.SZ   6.7925 -0.015258  480000         1\n",
       "           000002.SZ  10.0821  0.013463  430000         1\n",
       "           000008.SZ  42.9544 -0.122721  640000         4\n",
       "           000009.SZ  79.4778 -0.155903  510000         5\n",
       "           000027.SZ  20.4542 -0.041935  410000         2"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.data import DataView\n",
    "from jaqs_fxdayu.research import SignalDigger\n",
    "\n",
    "# 加载dataview数据集\n",
    "dv = DataView()\n",
    "dataview_folder = './data'\n",
    "dv.load_dataview(dataview_folder)\n",
    "\n",
    "# 计算signal_data\n",
    "sd = SignalDigger()\n",
    "sd.process_signal_before_analysis(signal=dv.get_ts(\"pe\"),\n",
    "                                   price=dv.get_ts(\"close_adj\"),\n",
    "                                   group=dv.get_ts(\"sw1\"),\n",
    "                                   n_quantiles=5,\n",
    "                                   period=15,\n",
    "                                   benchmark_price=dv.data_benchmark,\n",
    "                                   )\n",
    "signal_data = sd.signal_data\n",
    "signal_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## calc_signal_ic\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.calc_signal_ic(signal_data, by_group=False) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 计算每日ic\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是| pandas.DataFrame |trade_date+symbol为MultiIndex,columns至少包含signal(因子)、return(持有期相对/绝对收益)、group(分组/行业分类)--仅在by_group=True时必须|\n",
    "|by_group |否|bool|是否分组进行计算，默认为False|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "每日ic\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ic</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.288577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.341181</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.350174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.380677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.427141</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  ic\n",
       "trade_date          \n",
       "20170503   -0.288577\n",
       "20170504   -0.341181\n",
       "20170505   -0.350174\n",
       "20170508   -0.380677\n",
       "20170509   -0.427141"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import calc_signal_ic\n",
    "\n",
    "ic_data = calc_signal_ic(signal_data,by_group=False)\n",
    "ic_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>ic</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th>group</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">20170503</th>\n",
       "      <th>110000</th>\n",
       "      <td>-0.142857</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>210000</th>\n",
       "      <td>-0.452381</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220000</th>\n",
       "      <td>-0.285714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>230000</th>\n",
       "      <td>0.100000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240000</th>\n",
       "      <td>0.013986</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         ic\n",
       "trade_date group           \n",
       "20170503   110000 -0.142857\n",
       "           210000 -0.452381\n",
       "           220000 -0.285714\n",
       "           230000  0.100000\n",
       "           240000  0.013986"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "group_ic_data = calc_signal_ic(signal_data,by_group=True)\n",
    "group_ic_data.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## calc_ic_stats_table\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.calc_ic_stats_table(ic_data) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 根据每日ic计算总体ic统计结果\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|ic_data |是| pandas.DataFrame |trade_date为index,ic为columns。可通过calc_signal_ic计算得到|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "总体ic统计结果\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>IC Mean</th>\n",
       "      <th>IC Std.</th>\n",
       "      <th>t-stat(IC)</th>\n",
       "      <th>p-value(IC)</th>\n",
       "      <th>IC Skew</th>\n",
       "      <th>IC Kurtosis</th>\n",
       "      <th>Ann. IR</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ic</th>\n",
       "      <td>-0.030303</td>\n",
       "      <td>0.207642</td>\n",
       "      <td>-1.392159</td>\n",
       "      <td>0.167305</td>\n",
       "      <td>0.189897</td>\n",
       "      <td>-0.601332</td>\n",
       "      <td>-0.145938</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     IC Mean   IC Std.  t-stat(IC)  p-value(IC)   IC Skew  IC Kurtosis  \\\n",
       "ic -0.030303  0.207642   -1.392159     0.167305  0.189897    -0.601332   \n",
       "\n",
       "     Ann. IR  \n",
       "ic -0.145938  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import calc_ic_stats_table\n",
    "\n",
    "calc_ic_stats_table(ic_data)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## mean_information_coefficient\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.mean_information_coefficient(ic, by_time=None, by_group=False) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 输入ic,计算平均ic\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|ic |是| pandas.DataFrame |trade_date为index,ic为columns。可通过calc_signal_ic计算得到。注意：当by_group=True时，index需要为trade_date+group的MultiIndex（可以通过calc_signal_ic计算得到（设置by_group=True））|\n",
    "|by_time |否| str |支持pandas.TimeGrouper中的日期划分，如\"M\"（按月）,\"A\"（全部时段）,\"Q\"（按季度），\"W\"（按周）。默认求每日ic的所有样本的平均值|\n",
    "|by_group |否|bool|是否分组进行平均计算，默认为False|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "平均ic\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 示例一：按每3周求平均ic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ic</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2017-05-07</th>\n",
       "      <td>-0.326644</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-05-28</th>\n",
       "      <td>-0.225651</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-06-18</th>\n",
       "      <td>0.062981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-09</th>\n",
       "      <td>-0.193377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-07-30</th>\n",
       "      <td>0.217053</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-08-20</th>\n",
       "      <td>0.034492</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-09-10</th>\n",
       "      <td>-0.005609</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-10-01</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                  ic\n",
       "trade_date          \n",
       "2017-05-07 -0.326644\n",
       "2017-05-28 -0.225651\n",
       "2017-06-18  0.062981\n",
       "2017-07-09 -0.193377\n",
       "2017-07-30  0.217053\n",
       "2017-08-20  0.034492\n",
       "2017-09-10 -0.005609\n",
       "2017-10-01       NaN"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import mean_information_coefficient\n",
    "\n",
    "mean_information_coefficient(ic_data, by_time='3w')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 示例二：分组求每组的月平均ic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>ic</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th>group</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"28\" valign=\"top\">2017-05-31</th>\n",
       "      <th>110000</th>\n",
       "      <td>-0.323308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>210000</th>\n",
       "      <td>-0.464912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220000</th>\n",
       "      <td>-0.221515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>230000</th>\n",
       "      <td>-0.142105</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240000</th>\n",
       "      <td>-0.158263</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>270000</th>\n",
       "      <td>-0.236225</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>280000</th>\n",
       "      <td>-0.210836</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>330000</th>\n",
       "      <td>-0.253759</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340000</th>\n",
       "      <td>0.245614</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350000</th>\n",
       "      <td>-0.473684</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360000</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>370000</th>\n",
       "      <td>-0.318267</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410000</th>\n",
       "      <td>-0.339234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>420000</th>\n",
       "      <td>-0.127752</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>430000</th>\n",
       "      <td>-0.269569</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>450000</th>\n",
       "      <td>-0.091479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>460000</th>\n",
       "      <td>-0.589474</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>480000</th>\n",
       "      <td>-0.147984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>490000</th>\n",
       "      <td>-0.337726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>510000</th>\n",
       "      <td>0.684211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>610000</th>\n",
       "      <td>-0.684211</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>620000</th>\n",
       "      <td>-0.454309</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630000</th>\n",
       "      <td>-0.120301</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640000</th>\n",
       "      <td>0.262448</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>650000</th>\n",
       "      <td>-0.197368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>710000</th>\n",
       "      <td>-0.105139</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>720000</th>\n",
       "      <td>0.064058</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730000</th>\n",
       "      <td>-0.492196</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-06-30</th>\n",
       "      <th>110000</th>\n",
       "      <td>-0.179221</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>210000</th>\n",
       "      <td>0.496605</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">2017-08-31</th>\n",
       "      <th>720000</th>\n",
       "      <td>-0.215646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730000</th>\n",
       "      <td>-0.297784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"28\" valign=\"top\">2017-09-30</th>\n",
       "      <th>110000</th>\n",
       "      <td>0.172619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>210000</th>\n",
       "      <td>-0.214286</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>220000</th>\n",
       "      <td>0.240909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>230000</th>\n",
       "      <td>0.950000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>240000</th>\n",
       "      <td>0.009324</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>270000</th>\n",
       "      <td>0.019069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>280000</th>\n",
       "      <td>-0.104762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>330000</th>\n",
       "      <td>-0.238095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>340000</th>\n",
       "      <td>-0.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>350000</th>\n",
       "      <td>-1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>360000</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>370000</th>\n",
       "      <td>0.231336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>410000</th>\n",
       "      <td>-0.348485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>420000</th>\n",
       "      <td>0.331269</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>430000</th>\n",
       "      <td>0.110680</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>450000</th>\n",
       "      <td>0.476190</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>460000</th>\n",
       "      <td>0.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>480000</th>\n",
       "      <td>-0.642677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>490000</th>\n",
       "      <td>-0.125032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>510000</th>\n",
       "      <td>-0.233333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>610000</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>620000</th>\n",
       "      <td>-0.076007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>630000</th>\n",
       "      <td>0.013217</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>640000</th>\n",
       "      <td>-0.277610</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>650000</th>\n",
       "      <td>0.063889</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>710000</th>\n",
       "      <td>0.164695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>720000</th>\n",
       "      <td>-0.143014</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>730000</th>\n",
       "      <td>-0.444444</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>140 rows × 1 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                         ic\n",
       "trade_date group           \n",
       "2017-05-31 110000 -0.323308\n",
       "           210000 -0.464912\n",
       "           220000 -0.221515\n",
       "           230000 -0.142105\n",
       "           240000 -0.158263\n",
       "           270000 -0.236225\n",
       "           280000 -0.210836\n",
       "           330000 -0.253759\n",
       "           340000  0.245614\n",
       "           350000 -0.473684\n",
       "           360000       NaN\n",
       "           370000 -0.318267\n",
       "           410000 -0.339234\n",
       "           420000 -0.127752\n",
       "           430000 -0.269569\n",
       "           450000 -0.091479\n",
       "           460000 -0.589474\n",
       "           480000 -0.147984\n",
       "           490000 -0.337726\n",
       "           510000  0.684211\n",
       "           610000 -0.684211\n",
       "           620000 -0.454309\n",
       "           630000 -0.120301\n",
       "           640000  0.262448\n",
       "           650000 -0.197368\n",
       "           710000 -0.105139\n",
       "           720000  0.064058\n",
       "           730000 -0.492196\n",
       "2017-06-30 110000 -0.179221\n",
       "           210000  0.496605\n",
       "...                     ...\n",
       "2017-08-31 720000 -0.215646\n",
       "           730000 -0.297784\n",
       "2017-09-30 110000  0.172619\n",
       "           210000 -0.214286\n",
       "           220000  0.240909\n",
       "           230000  0.950000\n",
       "           240000  0.009324\n",
       "           270000  0.019069\n",
       "           280000 -0.104762\n",
       "           330000 -0.238095\n",
       "           340000 -0.333333\n",
       "           350000 -1.000000\n",
       "           360000       NaN\n",
       "           370000  0.231336\n",
       "           410000 -0.348485\n",
       "           420000  0.331269\n",
       "           430000  0.110680\n",
       "           450000  0.476190\n",
       "           460000  0.500000\n",
       "           480000 -0.642677\n",
       "           490000 -0.125032\n",
       "           510000 -0.233333\n",
       "           610000       NaN\n",
       "           620000 -0.076007\n",
       "           630000  0.013217\n",
       "           640000 -0.277610\n",
       "           650000  0.063889\n",
       "           710000  0.164695\n",
       "           720000 -0.143014\n",
       "           730000 -0.444444\n",
       "\n",
       "[140 rows x 1 columns]"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_information_coefficient(group_ic_data, by_group=True, by_time=\"M\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## calc_period_wise_weighted_signal_return\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.calc_period_wise_weighted_signal_return(signal_data, weight_method) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 根据signal_data构建投资组合，计算投资组合的每日调仓收益\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是| pandas.DataFrame |trade_date+symbol为MultiIndex,columns至少包含signal(因子)、return(持有期相对/绝对收益)|\n",
    "|weight_method |是| str |支持四种投资组合构建方式：'equal_weight'(对signal_data中的每一只股票等资金买入), 'long_only'（只做多signal值为正的股票，并按signal的大小加权构建多头组合）, 'short_only'（只做空signal值为负的股票，并按signal的大小加权构建空头组合）,'long_short'（做多signal为正，做空signal为负的股票，按signal的大小加权）|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "投资组合的每日调仓收益\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.066372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.070300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.065394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.064365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.078423</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              return\n",
       "trade_date          \n",
       "20170503   -0.066372\n",
       "20170504   -0.070300\n",
       "20170505   -0.065394\n",
       "20170508   -0.064365\n",
       "20170509   -0.078423"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import calc_period_wise_weighted_signal_return\n",
    "\n",
    "daily_return = calc_period_wise_weighted_signal_return(signal_data, weight_method=\"long_only\")\n",
    "daily_return.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## regress_period_wise_signal_return\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.regress_period_wise_signal_return(signal_data) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 对signal_data中的signal和return进行横截面回归（OLS）,计算每期的因子收益（回归系数）\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是| pandas.DataFrame |trade_date+symbol为MultiIndex,columns至少包含signal(因子)、return(持有期相对/绝对收益)|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "每期的因子收益（回归系数）\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.000093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.000091</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.000090</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.000095</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.000104</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   0\n",
       "trade_date          \n",
       "20170503   -0.000093\n",
       "20170504   -0.000091\n",
       "20170505   -0.000090\n",
       "20170508   -0.000095\n",
       "20170509   -0.000104"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import regress_period_wise_signal_return\n",
    "\n",
    "regress_period_wise_signal_return(signal_data).head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## calc_quantile_return_mean_std\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.calc_quantile_return_mean_std(signal_data, time_series=False) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 将股票按quantile分组分别等权买入持有，计算每组的平均持有收益（每日）和持有收益的标准差\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|signal_data |是| pandas.DataFrame |trade_date+symbol为MultiIndex,columns至少包含signal(因子)、return(持有期相对/绝对收益)、quantile(按因子值分组)|\n",
    "|time_series |否| bool |是否展示每组每天的收益，默认为False|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "每组（quantile）的平均持有收益（每日）和持有收益的标准差\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 示例一：展示每组的平均持有收益（每日）和持有收益的标准差（time_series=False）\n",
    "返回pandas.DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quantile</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>-0.000813</td>\n",
       "      <td>0.051852</td>\n",
       "      <td>6996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.000514</td>\n",
       "      <td>0.051664</td>\n",
       "      <td>6996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>-0.005477</td>\n",
       "      <td>0.057944</td>\n",
       "      <td>6996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.010762</td>\n",
       "      <td>0.063931</td>\n",
       "      <td>6996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>-0.000114</td>\n",
       "      <td>0.079470</td>\n",
       "      <td>6996</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              mean       std  count\n",
       "quantile                           \n",
       "1        -0.000813  0.051852   6996\n",
       "2        -0.000514  0.051664   6996\n",
       "3        -0.005477  0.057944   6996\n",
       "4        -0.010762  0.063931   6996\n",
       "5        -0.000114  0.079470   6996"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import calc_quantile_return_mean_std\n",
    "\n",
    "calc_quantile_return_mean_std(signal_data, time_series=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 示例二：展示每组每日的持有收益和持有收益的标准差（time_series=True）\n",
    "返回dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dict_keys([1, 2, 3, 4, 5])\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>count</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.005253</td>\n",
       "      <td>0.068051</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.004897</td>\n",
       "      <td>0.076375</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.003048</td>\n",
       "      <td>0.074683</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.001248</td>\n",
       "      <td>0.063775</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.002070</td>\n",
       "      <td>0.072706</td>\n",
       "      <td>66</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                mean       std  count\n",
       "trade_date                           \n",
       "20170503   -0.005253  0.068051     66\n",
       "20170504   -0.004897  0.076375     66\n",
       "20170505   -0.003048  0.074683     66\n",
       "20170508   -0.001248  0.063775     66\n",
       "20170509   -0.002070  0.072706     66"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = calc_quantile_return_mean_std(signal_data, time_series=True)\n",
    "print(result.keys())\n",
    "result[1].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## price2ret\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.price2ret(prices, period=5, axis=None, compound=True) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 将价格序列转化为定期调仓收益序列\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|price |是| pandas.DataFrame/pandas.Series |时间为索引的价格表|\n",
    "|period |否| int |调仓周期，默认为5|\n",
    "|axis |否| int |{0, 1, None}，将表格按某个维度进行收益计算（横向/纵向）,默认纵向计算|\n",
    "|compound |否| bool |收益计算是否为复利。单利：（相对表格第一行的收益）；复利（相对上一期的收益），默认为True 复利模式|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "收益序列\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>symbol</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000027.SZ</th>\n",
       "      <th>000039.SZ</th>\n",
       "      <th>000060.SZ</th>\n",
       "      <th>000061.SZ</th>\n",
       "      <th>000063.SZ</th>\n",
       "      <th>000069.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>601988.SH</th>\n",
       "      <th>601989.SH</th>\n",
       "      <th>601992.SH</th>\n",
       "      <th>601997.SH</th>\n",
       "      <th>601998.SH</th>\n",
       "      <th>603000.SH</th>\n",
       "      <th>603160.SH</th>\n",
       "      <th>603858.SH</th>\n",
       "      <th>603885.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170502</th>\n",
       "      <td>898.67562</td>\n",
       "      <td>2373.25440</td>\n",
       "      <td>210.898632</td>\n",
       "      <td>85.962653</td>\n",
       "      <td>94.89530</td>\n",
       "      <td>378.975476</td>\n",
       "      <td>241.71550</td>\n",
       "      <td>200.562672</td>\n",
       "      <td>299.850012</td>\n",
       "      <td>318.911580</td>\n",
       "      <td>...</td>\n",
       "      <td>5.597708</td>\n",
       "      <td>11.717117</td>\n",
       "      <td>17.382915</td>\n",
       "      <td>15.67</td>\n",
       "      <td>7.856221</td>\n",
       "      <td>60.301733</td>\n",
       "      <td>97.88</td>\n",
       "      <td>80.49</td>\n",
       "      <td>45.397002</td>\n",
       "      <td>14.498578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>895.65993</td>\n",
       "      <td>2331.22802</td>\n",
       "      <td>209.312928</td>\n",
       "      <td>85.265390</td>\n",
       "      <td>93.66644</td>\n",
       "      <td>378.030400</td>\n",
       "      <td>239.12148</td>\n",
       "      <td>198.410712</td>\n",
       "      <td>302.909706</td>\n",
       "      <td>314.186964</td>\n",
       "      <td>...</td>\n",
       "      <td>5.550537</td>\n",
       "      <td>11.532463</td>\n",
       "      <td>16.034422</td>\n",
       "      <td>15.71</td>\n",
       "      <td>7.777265</td>\n",
       "      <td>60.998626</td>\n",
       "      <td>97.67</td>\n",
       "      <td>80.53</td>\n",
       "      <td>45.498789</td>\n",
       "      <td>14.450080</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>878.57102</td>\n",
       "      <td>2351.00514</td>\n",
       "      <td>208.255792</td>\n",
       "      <td>85.066171</td>\n",
       "      <td>93.80298</td>\n",
       "      <td>372.123675</td>\n",
       "      <td>234.64090</td>\n",
       "      <td>196.473948</td>\n",
       "      <td>303.079689</td>\n",
       "      <td>314.974400</td>\n",
       "      <td>...</td>\n",
       "      <td>5.550537</td>\n",
       "      <td>11.364596</td>\n",
       "      <td>16.687598</td>\n",
       "      <td>15.61</td>\n",
       "      <td>7.724627</td>\n",
       "      <td>59.276890</td>\n",
       "      <td>96.99</td>\n",
       "      <td>79.62</td>\n",
       "      <td>45.295215</td>\n",
       "      <td>14.094485</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>867.51349</td>\n",
       "      <td>2334.93623</td>\n",
       "      <td>199.270136</td>\n",
       "      <td>82.277117</td>\n",
       "      <td>92.84720</td>\n",
       "      <td>359.128880</td>\n",
       "      <td>232.99016</td>\n",
       "      <td>195.613164</td>\n",
       "      <td>299.170080</td>\n",
       "      <td>310.249784</td>\n",
       "      <td>...</td>\n",
       "      <td>5.566261</td>\n",
       "      <td>11.129582</td>\n",
       "      <td>16.371545</td>\n",
       "      <td>15.20</td>\n",
       "      <td>7.685148</td>\n",
       "      <td>57.842111</td>\n",
       "      <td>95.11</td>\n",
       "      <td>78.51</td>\n",
       "      <td>45.824507</td>\n",
       "      <td>13.803544</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>861.48211</td>\n",
       "      <td>2283.02129</td>\n",
       "      <td>193.191604</td>\n",
       "      <td>80.284935</td>\n",
       "      <td>90.93564</td>\n",
       "      <td>355.112307</td>\n",
       "      <td>233.93344</td>\n",
       "      <td>196.689144</td>\n",
       "      <td>289.481049</td>\n",
       "      <td>308.281194</td>\n",
       "      <td>...</td>\n",
       "      <td>5.613432</td>\n",
       "      <td>10.777061</td>\n",
       "      <td>14.812351</td>\n",
       "      <td>14.83</td>\n",
       "      <td>7.724627</td>\n",
       "      <td>57.678136</td>\n",
       "      <td>97.00</td>\n",
       "      <td>76.60</td>\n",
       "      <td>45.030569</td>\n",
       "      <td>13.868197</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 330 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "symbol      000001.SZ   000002.SZ   000008.SZ  000009.SZ  000027.SZ  \\\n",
       "trade_date                                                            \n",
       "20170502    898.67562  2373.25440  210.898632  85.962653   94.89530   \n",
       "20170503    895.65993  2331.22802  209.312928  85.265390   93.66644   \n",
       "20170504    878.57102  2351.00514  208.255792  85.066171   93.80298   \n",
       "20170505    867.51349  2334.93623  199.270136  82.277117   92.84720   \n",
       "20170508    861.48211  2283.02129  193.191604  80.284935   90.93564   \n",
       "\n",
       "symbol       000039.SZ  000060.SZ   000061.SZ   000063.SZ   000069.SZ  \\\n",
       "trade_date                                                              \n",
       "20170502    378.975476  241.71550  200.562672  299.850012  318.911580   \n",
       "20170503    378.030400  239.12148  198.410712  302.909706  314.186964   \n",
       "20170504    372.123675  234.64090  196.473948  303.079689  314.974400   \n",
       "20170505    359.128880  232.99016  195.613164  299.170080  310.249784   \n",
       "20170508    355.112307  233.93344  196.689144  289.481049  308.281194   \n",
       "\n",
       "symbol        ...      601988.SH  601989.SH  601992.SH  601997.SH  601998.SH  \\\n",
       "trade_date    ...                                                              \n",
       "20170502      ...       5.597708  11.717117  17.382915      15.67   7.856221   \n",
       "20170503      ...       5.550537  11.532463  16.034422      15.71   7.777265   \n",
       "20170504      ...       5.550537  11.364596  16.687598      15.61   7.724627   \n",
       "20170505      ...       5.566261  11.129582  16.371545      15.20   7.685148   \n",
       "20170508      ...       5.613432  10.777061  14.812351      14.83   7.724627   \n",
       "\n",
       "symbol      603000.SH  603160.SH  603858.SH  603885.SH  603993.SH  \n",
       "trade_date                                                         \n",
       "20170502    60.301733      97.88      80.49  45.397002  14.498578  \n",
       "20170503    60.998626      97.67      80.53  45.498789  14.450080  \n",
       "20170504    59.276890      96.99      79.62  45.295215  14.094485  \n",
       "20170505    57.842111      95.11      78.51  45.824507  13.803544  \n",
       "20170508    57.678136      97.00      76.60  45.030569  13.868197  \n",
       "\n",
       "[5 rows x 330 columns]"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prices = dv.get_ts(\"close_adj\")\n",
    "prices.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>symbol</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000027.SZ</th>\n",
       "      <th>000039.SZ</th>\n",
       "      <th>000060.SZ</th>\n",
       "      <th>000061.SZ</th>\n",
       "      <th>000063.SZ</th>\n",
       "      <th>000069.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>601988.SH</th>\n",
       "      <th>601989.SH</th>\n",
       "      <th>601992.SH</th>\n",
       "      <th>601997.SH</th>\n",
       "      <th>601998.SH</th>\n",
       "      <th>603000.SH</th>\n",
       "      <th>603160.SH</th>\n",
       "      <th>603858.SH</th>\n",
       "      <th>603885.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.033557</td>\n",
       "      <td>-0.030208</td>\n",
       "      <td>-0.081454</td>\n",
       "      <td>-0.063731</td>\n",
       "      <td>-0.035971</td>\n",
       "      <td>-0.053616</td>\n",
       "      <td>-0.015610</td>\n",
       "      <td>-0.020386</td>\n",
       "      <td>-0.027211</td>\n",
       "      <td>-0.032099</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002809</td>\n",
       "      <td>-0.070201</td>\n",
       "      <td>-0.134545</td>\n",
       "      <td>-0.019145</td>\n",
       "      <td>-0.013400</td>\n",
       "      <td>-0.037390</td>\n",
       "      <td>-0.007662</td>\n",
       "      <td>-0.040253</td>\n",
       "      <td>-0.033632</td>\n",
       "      <td>-0.043479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170510</th>\n",
       "      <td>-0.026936</td>\n",
       "      <td>-0.010074</td>\n",
       "      <td>-0.070707</td>\n",
       "      <td>-0.091121</td>\n",
       "      <td>-0.040816</td>\n",
       "      <td>-0.078750</td>\n",
       "      <td>-0.038462</td>\n",
       "      <td>-0.034707</td>\n",
       "      <td>-0.056678</td>\n",
       "      <td>-0.016291</td>\n",
       "      <td>...</td>\n",
       "      <td>0.014164</td>\n",
       "      <td>-0.082969</td>\n",
       "      <td>-0.137976</td>\n",
       "      <td>0.004456</td>\n",
       "      <td>-0.003384</td>\n",
       "      <td>-0.061828</td>\n",
       "      <td>-0.058360</td>\n",
       "      <td>-0.066559</td>\n",
       "      <td>-0.052349</td>\n",
       "      <td>-0.073826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170511</th>\n",
       "      <td>-0.004577</td>\n",
       "      <td>0.011567</td>\n",
       "      <td>-0.058376</td>\n",
       "      <td>-0.093677</td>\n",
       "      <td>-0.061135</td>\n",
       "      <td>-0.060952</td>\n",
       "      <td>-0.026149</td>\n",
       "      <td>-0.059146</td>\n",
       "      <td>-0.029725</td>\n",
       "      <td>-0.012500</td>\n",
       "      <td>...</td>\n",
       "      <td>0.025496</td>\n",
       "      <td>-0.063516</td>\n",
       "      <td>-0.140152</td>\n",
       "      <td>0.016015</td>\n",
       "      <td>0.017036</td>\n",
       "      <td>-0.063624</td>\n",
       "      <td>-0.044334</td>\n",
       "      <td>-0.075232</td>\n",
       "      <td>-0.069213</td>\n",
       "      <td>-0.043578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170512</th>\n",
       "      <td>0.031286</td>\n",
       "      <td>0.026998</td>\n",
       "      <td>-0.025199</td>\n",
       "      <td>-0.069007</td>\n",
       "      <td>-0.051471</td>\n",
       "      <td>-0.002632</td>\n",
       "      <td>-0.026356</td>\n",
       "      <td>-0.051705</td>\n",
       "      <td>-0.020455</td>\n",
       "      <td>0.019036</td>\n",
       "      <td>...</td>\n",
       "      <td>0.036723</td>\n",
       "      <td>-0.051282</td>\n",
       "      <td>-0.120978</td>\n",
       "      <td>0.063816</td>\n",
       "      <td>0.051370</td>\n",
       "      <td>-0.046775</td>\n",
       "      <td>-0.016823</td>\n",
       "      <td>-0.068654</td>\n",
       "      <td>-0.084851</td>\n",
       "      <td>-0.035129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170515</th>\n",
       "      <td>0.033839</td>\n",
       "      <td>0.047103</td>\n",
       "      <td>0.006840</td>\n",
       "      <td>-0.042184</td>\n",
       "      <td>-0.027027</td>\n",
       "      <td>0.005323</td>\n",
       "      <td>-0.026237</td>\n",
       "      <td>-0.054705</td>\n",
       "      <td>0.024075</td>\n",
       "      <td>0.040868</td>\n",
       "      <td>...</td>\n",
       "      <td>0.025210</td>\n",
       "      <td>-0.031153</td>\n",
       "      <td>-0.036984</td>\n",
       "      <td>0.078220</td>\n",
       "      <td>0.034072</td>\n",
       "      <td>-0.039090</td>\n",
       "      <td>-0.035258</td>\n",
       "      <td>-0.041775</td>\n",
       "      <td>-0.064647</td>\n",
       "      <td>-0.025641</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 330 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "symbol      000001.SZ  000002.SZ  000008.SZ  000009.SZ  000027.SZ  000039.SZ  \\\n",
       "trade_date                                                                     \n",
       "20170509    -0.033557  -0.030208  -0.081454  -0.063731  -0.035971  -0.053616   \n",
       "20170510    -0.026936  -0.010074  -0.070707  -0.091121  -0.040816  -0.078750   \n",
       "20170511    -0.004577   0.011567  -0.058376  -0.093677  -0.061135  -0.060952   \n",
       "20170512     0.031286   0.026998  -0.025199  -0.069007  -0.051471  -0.002632   \n",
       "20170515     0.033839   0.047103   0.006840  -0.042184  -0.027027   0.005323   \n",
       "\n",
       "symbol      000060.SZ  000061.SZ  000063.SZ  000069.SZ    ...      601988.SH  \\\n",
       "trade_date                                                ...                  \n",
       "20170509    -0.015610  -0.020386  -0.027211  -0.032099    ...       0.002809   \n",
       "20170510    -0.038462  -0.034707  -0.056678  -0.016291    ...       0.014164   \n",
       "20170511    -0.026149  -0.059146  -0.029725  -0.012500    ...       0.025496   \n",
       "20170512    -0.026356  -0.051705  -0.020455   0.019036    ...       0.036723   \n",
       "20170515    -0.026237  -0.054705   0.024075   0.040868    ...       0.025210   \n",
       "\n",
       "symbol      601989.SH  601992.SH  601997.SH  601998.SH  603000.SH  603160.SH  \\\n",
       "trade_date                                                                     \n",
       "20170509    -0.070201  -0.134545  -0.019145  -0.013400  -0.037390  -0.007662   \n",
       "20170510    -0.082969  -0.137976   0.004456  -0.003384  -0.061828  -0.058360   \n",
       "20170511    -0.063516  -0.140152   0.016015   0.017036  -0.063624  -0.044334   \n",
       "20170512    -0.051282  -0.120978   0.063816   0.051370  -0.046775  -0.016823   \n",
       "20170515    -0.031153  -0.036984   0.078220   0.034072  -0.039090  -0.035258   \n",
       "\n",
       "symbol      603858.SH  603885.SH  603993.SH  \n",
       "trade_date                                   \n",
       "20170509    -0.040253  -0.033632  -0.043479  \n",
       "20170510    -0.066559  -0.052349  -0.073826  \n",
       "20170511    -0.075232  -0.069213  -0.043578  \n",
       "20170512    -0.068654  -0.084851  -0.035129  \n",
       "20170515    -0.041775  -0.064647  -0.025641  \n",
       "\n",
       "[5 rows x 330 columns]"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import price2ret\n",
    "\n",
    "ret = price2ret(prices, period=5, compound=True)\n",
    "ret.dropna().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## ret2cum\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.ret2cum(ret, compound=True, axis=None) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 将收益序列转化为累积收益序列\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|ret |是| pandas.DataFrame/pandas.Series |时间为索引的收益表|\n",
    "|compound |否| bool |收益计算是否为复利。单利：（每期累加的收益）；复利（每期累乘的收益），默认为True 复利模式|\n",
    "|axis |否| int |{0, 1, None}，将表格按某个维度进行收益计算（横向/纵向）,默认纵向计算|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "累积收益序列\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>symbol</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000027.SZ</th>\n",
       "      <th>000039.SZ</th>\n",
       "      <th>000060.SZ</th>\n",
       "      <th>000061.SZ</th>\n",
       "      <th>000063.SZ</th>\n",
       "      <th>000069.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>601988.SH</th>\n",
       "      <th>601989.SH</th>\n",
       "      <th>601992.SH</th>\n",
       "      <th>601997.SH</th>\n",
       "      <th>601998.SH</th>\n",
       "      <th>603000.SH</th>\n",
       "      <th>603160.SH</th>\n",
       "      <th>603858.SH</th>\n",
       "      <th>603885.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.033557</td>\n",
       "      <td>-0.030208</td>\n",
       "      <td>-0.081454</td>\n",
       "      <td>-0.063731</td>\n",
       "      <td>-0.035971</td>\n",
       "      <td>-0.053616</td>\n",
       "      <td>-0.015610</td>\n",
       "      <td>-0.020386</td>\n",
       "      <td>-0.027211</td>\n",
       "      <td>-0.032099</td>\n",
       "      <td>...</td>\n",
       "      <td>0.002809</td>\n",
       "      <td>-0.070201</td>\n",
       "      <td>-0.134545</td>\n",
       "      <td>-0.019145</td>\n",
       "      <td>-0.013400</td>\n",
       "      <td>-0.037390</td>\n",
       "      <td>-0.007662</td>\n",
       "      <td>-0.040253</td>\n",
       "      <td>-0.033632</td>\n",
       "      <td>-0.043479</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170510</th>\n",
       "      <td>-0.059589</td>\n",
       "      <td>-0.039978</td>\n",
       "      <td>-0.146401</td>\n",
       "      <td>-0.149045</td>\n",
       "      <td>-0.075319</td>\n",
       "      <td>-0.128144</td>\n",
       "      <td>-0.053471</td>\n",
       "      <td>-0.054386</td>\n",
       "      <td>-0.082347</td>\n",
       "      <td>-0.047867</td>\n",
       "      <td>...</td>\n",
       "      <td>0.017013</td>\n",
       "      <td>-0.147346</td>\n",
       "      <td>-0.253958</td>\n",
       "      <td>-0.014774</td>\n",
       "      <td>-0.016739</td>\n",
       "      <td>-0.096906</td>\n",
       "      <td>-0.065575</td>\n",
       "      <td>-0.104133</td>\n",
       "      <td>-0.084221</td>\n",
       "      <td>-0.114094</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170511</th>\n",
       "      <td>-0.063893</td>\n",
       "      <td>-0.028874</td>\n",
       "      <td>-0.196231</td>\n",
       "      <td>-0.228760</td>\n",
       "      <td>-0.131850</td>\n",
       "      <td>-0.181285</td>\n",
       "      <td>-0.078221</td>\n",
       "      <td>-0.110315</td>\n",
       "      <td>-0.109624</td>\n",
       "      <td>-0.059768</td>\n",
       "      <td>...</td>\n",
       "      <td>0.042943</td>\n",
       "      <td>-0.201502</td>\n",
       "      <td>-0.358517</td>\n",
       "      <td>0.001004</td>\n",
       "      <td>0.000012</td>\n",
       "      <td>-0.154364</td>\n",
       "      <td>-0.107002</td>\n",
       "      <td>-0.171531</td>\n",
       "      <td>-0.147605</td>\n",
       "      <td>-0.152700</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170512</th>\n",
       "      <td>-0.034606</td>\n",
       "      <td>-0.002655</td>\n",
       "      <td>-0.216485</td>\n",
       "      <td>-0.281981</td>\n",
       "      <td>-0.176534</td>\n",
       "      <td>-0.183440</td>\n",
       "      <td>-0.102515</td>\n",
       "      <td>-0.156316</td>\n",
       "      <td>-0.127836</td>\n",
       "      <td>-0.041870</td>\n",
       "      <td>...</td>\n",
       "      <td>0.081243</td>\n",
       "      <td>-0.242451</td>\n",
       "      <td>-0.436122</td>\n",
       "      <td>0.064884</td>\n",
       "      <td>0.051382</td>\n",
       "      <td>-0.193919</td>\n",
       "      <td>-0.122025</td>\n",
       "      <td>-0.228409</td>\n",
       "      <td>-0.219932</td>\n",
       "      <td>-0.182465</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170515</th>\n",
       "      <td>-0.001938</td>\n",
       "      <td>0.044323</td>\n",
       "      <td>-0.211126</td>\n",
       "      <td>-0.312270</td>\n",
       "      <td>-0.198790</td>\n",
       "      <td>-0.179094</td>\n",
       "      <td>-0.126063</td>\n",
       "      <td>-0.202470</td>\n",
       "      <td>-0.106839</td>\n",
       "      <td>-0.002713</td>\n",
       "      <td>...</td>\n",
       "      <td>0.108501</td>\n",
       "      <td>-0.266051</td>\n",
       "      <td>-0.456977</td>\n",
       "      <td>0.148179</td>\n",
       "      <td>0.087204</td>\n",
       "      <td>-0.225429</td>\n",
       "      <td>-0.152980</td>\n",
       "      <td>-0.260642</td>\n",
       "      <td>-0.270361</td>\n",
       "      <td>-0.203427</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 330 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "symbol      000001.SZ  000002.SZ  000008.SZ  000009.SZ  000027.SZ  000039.SZ  \\\n",
       "trade_date                                                                     \n",
       "20170509    -0.033557  -0.030208  -0.081454  -0.063731  -0.035971  -0.053616   \n",
       "20170510    -0.059589  -0.039978  -0.146401  -0.149045  -0.075319  -0.128144   \n",
       "20170511    -0.063893  -0.028874  -0.196231  -0.228760  -0.131850  -0.181285   \n",
       "20170512    -0.034606  -0.002655  -0.216485  -0.281981  -0.176534  -0.183440   \n",
       "20170515    -0.001938   0.044323  -0.211126  -0.312270  -0.198790  -0.179094   \n",
       "\n",
       "symbol      000060.SZ  000061.SZ  000063.SZ  000069.SZ    ...      601988.SH  \\\n",
       "trade_date                                                ...                  \n",
       "20170509    -0.015610  -0.020386  -0.027211  -0.032099    ...       0.002809   \n",
       "20170510    -0.053471  -0.054386  -0.082347  -0.047867    ...       0.017013   \n",
       "20170511    -0.078221  -0.110315  -0.109624  -0.059768    ...       0.042943   \n",
       "20170512    -0.102515  -0.156316  -0.127836  -0.041870    ...       0.081243   \n",
       "20170515    -0.126063  -0.202470  -0.106839  -0.002713    ...       0.108501   \n",
       "\n",
       "symbol      601989.SH  601992.SH  601997.SH  601998.SH  603000.SH  603160.SH  \\\n",
       "trade_date                                                                     \n",
       "20170509    -0.070201  -0.134545  -0.019145  -0.013400  -0.037390  -0.007662   \n",
       "20170510    -0.147346  -0.253958  -0.014774  -0.016739  -0.096906  -0.065575   \n",
       "20170511    -0.201502  -0.358517   0.001004   0.000012  -0.154364  -0.107002   \n",
       "20170512    -0.242451  -0.436122   0.064884   0.051382  -0.193919  -0.122025   \n",
       "20170515    -0.266051  -0.456977   0.148179   0.087204  -0.225429  -0.152980   \n",
       "\n",
       "symbol      603858.SH  603885.SH  603993.SH  \n",
       "trade_date                                   \n",
       "20170509    -0.040253  -0.033632  -0.043479  \n",
       "20170510    -0.104133  -0.084221  -0.114094  \n",
       "20170511    -0.171531  -0.147605  -0.152700  \n",
       "20170512    -0.228409  -0.219932  -0.182465  \n",
       "20170515    -0.260642  -0.270361  -0.203427  \n",
       "\n",
       "[5 rows x 330 columns]"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import ret2cum\n",
    "\n",
    "cum = ret2cum(ret, compound=True)\n",
    "cum.dropna().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## cum2ret\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.cum2ret(cum, period=1, axis=None, compound=True) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 将累积收益序列转化为收益序列\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|cum |是| pandas.DataFrame/pandas.Series |时间为索引的累积收益表|\n",
    "|period |否| int |通常为1。累积收益的累积间隔周期。默认为1|\n",
    "|compound |否| bool |收益计算是否为复利。单利：（每期累加的收益）；复利（每期累乘的收益），默认为True 复利模式|\n",
    "|axis |否| int |{0, 1, None}，将表格按某个维度进行收益计算（横向/纵向）,默认纵向计算|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "收益序列\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>symbol</th>\n",
       "      <th>000001.SZ</th>\n",
       "      <th>000002.SZ</th>\n",
       "      <th>000008.SZ</th>\n",
       "      <th>000009.SZ</th>\n",
       "      <th>000027.SZ</th>\n",
       "      <th>000039.SZ</th>\n",
       "      <th>000060.SZ</th>\n",
       "      <th>000061.SZ</th>\n",
       "      <th>000063.SZ</th>\n",
       "      <th>000069.SZ</th>\n",
       "      <th>...</th>\n",
       "      <th>601988.SH</th>\n",
       "      <th>601989.SH</th>\n",
       "      <th>601992.SH</th>\n",
       "      <th>601997.SH</th>\n",
       "      <th>601998.SH</th>\n",
       "      <th>603000.SH</th>\n",
       "      <th>603160.SH</th>\n",
       "      <th>603858.SH</th>\n",
       "      <th>603885.SH</th>\n",
       "      <th>603993.SH</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170510</th>\n",
       "      <td>-0.026936</td>\n",
       "      <td>-0.010074</td>\n",
       "      <td>-0.070707</td>\n",
       "      <td>-0.091121</td>\n",
       "      <td>-0.040816</td>\n",
       "      <td>-0.078750</td>\n",
       "      <td>-0.038462</td>\n",
       "      <td>-0.034707</td>\n",
       "      <td>-0.056678</td>\n",
       "      <td>-0.016291</td>\n",
       "      <td>...</td>\n",
       "      <td>0.014164</td>\n",
       "      <td>-0.082969</td>\n",
       "      <td>-0.137976</td>\n",
       "      <td>0.004456</td>\n",
       "      <td>-0.003384</td>\n",
       "      <td>-0.061828</td>\n",
       "      <td>-0.058360</td>\n",
       "      <td>-0.066559</td>\n",
       "      <td>-0.052349</td>\n",
       "      <td>-0.073826</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170511</th>\n",
       "      <td>-0.004577</td>\n",
       "      <td>0.011567</td>\n",
       "      <td>-0.058376</td>\n",
       "      <td>-0.093677</td>\n",
       "      <td>-0.061135</td>\n",
       "      <td>-0.060952</td>\n",
       "      <td>-0.026149</td>\n",
       "      <td>-0.059146</td>\n",
       "      <td>-0.029725</td>\n",
       "      <td>-0.012500</td>\n",
       "      <td>...</td>\n",
       "      <td>0.025496</td>\n",
       "      <td>-0.063516</td>\n",
       "      <td>-0.140152</td>\n",
       "      <td>0.016015</td>\n",
       "      <td>0.017036</td>\n",
       "      <td>-0.063624</td>\n",
       "      <td>-0.044334</td>\n",
       "      <td>-0.075232</td>\n",
       "      <td>-0.069213</td>\n",
       "      <td>-0.043578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170512</th>\n",
       "      <td>0.031286</td>\n",
       "      <td>0.026998</td>\n",
       "      <td>-0.025199</td>\n",
       "      <td>-0.069007</td>\n",
       "      <td>-0.051471</td>\n",
       "      <td>-0.002632</td>\n",
       "      <td>-0.026356</td>\n",
       "      <td>-0.051705</td>\n",
       "      <td>-0.020455</td>\n",
       "      <td>0.019036</td>\n",
       "      <td>...</td>\n",
       "      <td>0.036723</td>\n",
       "      <td>-0.051282</td>\n",
       "      <td>-0.120978</td>\n",
       "      <td>0.063816</td>\n",
       "      <td>0.051370</td>\n",
       "      <td>-0.046775</td>\n",
       "      <td>-0.016823</td>\n",
       "      <td>-0.068654</td>\n",
       "      <td>-0.084851</td>\n",
       "      <td>-0.035129</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170515</th>\n",
       "      <td>0.033839</td>\n",
       "      <td>0.047103</td>\n",
       "      <td>0.006840</td>\n",
       "      <td>-0.042184</td>\n",
       "      <td>-0.027027</td>\n",
       "      <td>0.005323</td>\n",
       "      <td>-0.026237</td>\n",
       "      <td>-0.054705</td>\n",
       "      <td>0.024075</td>\n",
       "      <td>0.040868</td>\n",
       "      <td>...</td>\n",
       "      <td>0.025210</td>\n",
       "      <td>-0.031153</td>\n",
       "      <td>-0.036984</td>\n",
       "      <td>0.078220</td>\n",
       "      <td>0.034072</td>\n",
       "      <td>-0.039090</td>\n",
       "      <td>-0.035258</td>\n",
       "      <td>-0.041775</td>\n",
       "      <td>-0.064647</td>\n",
       "      <td>-0.025641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170516</th>\n",
       "      <td>0.023148</td>\n",
       "      <td>0.032760</td>\n",
       "      <td>0.006821</td>\n",
       "      <td>-0.024752</td>\n",
       "      <td>-0.008955</td>\n",
       "      <td>0.012516</td>\n",
       "      <td>-0.022761</td>\n",
       "      <td>-0.037240</td>\n",
       "      <td>0.103730</td>\n",
       "      <td>0.040816</td>\n",
       "      <td>...</td>\n",
       "      <td>0.016807</td>\n",
       "      <td>-0.030817</td>\n",
       "      <td>0.029412</td>\n",
       "      <td>0.036435</td>\n",
       "      <td>0.028862</td>\n",
       "      <td>-0.027542</td>\n",
       "      <td>0.018429</td>\n",
       "      <td>-0.035728</td>\n",
       "      <td>-0.028770</td>\n",
       "      <td>0.020979</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 330 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "symbol      000001.SZ  000002.SZ  000008.SZ  000009.SZ  000027.SZ  000039.SZ  \\\n",
       "trade_date                                                                     \n",
       "20170510    -0.026936  -0.010074  -0.070707  -0.091121  -0.040816  -0.078750   \n",
       "20170511    -0.004577   0.011567  -0.058376  -0.093677  -0.061135  -0.060952   \n",
       "20170512     0.031286   0.026998  -0.025199  -0.069007  -0.051471  -0.002632   \n",
       "20170515     0.033839   0.047103   0.006840  -0.042184  -0.027027   0.005323   \n",
       "20170516     0.023148   0.032760   0.006821  -0.024752  -0.008955   0.012516   \n",
       "\n",
       "symbol      000060.SZ  000061.SZ  000063.SZ  000069.SZ    ...      601988.SH  \\\n",
       "trade_date                                                ...                  \n",
       "20170510    -0.038462  -0.034707  -0.056678  -0.016291    ...       0.014164   \n",
       "20170511    -0.026149  -0.059146  -0.029725  -0.012500    ...       0.025496   \n",
       "20170512    -0.026356  -0.051705  -0.020455   0.019036    ...       0.036723   \n",
       "20170515    -0.026237  -0.054705   0.024075   0.040868    ...       0.025210   \n",
       "20170516    -0.022761  -0.037240   0.103730   0.040816    ...       0.016807   \n",
       "\n",
       "symbol      601989.SH  601992.SH  601997.SH  601998.SH  603000.SH  603160.SH  \\\n",
       "trade_date                                                                     \n",
       "20170510    -0.082969  -0.137976   0.004456  -0.003384  -0.061828  -0.058360   \n",
       "20170511    -0.063516  -0.140152   0.016015   0.017036  -0.063624  -0.044334   \n",
       "20170512    -0.051282  -0.120978   0.063816   0.051370  -0.046775  -0.016823   \n",
       "20170515    -0.031153  -0.036984   0.078220   0.034072  -0.039090  -0.035258   \n",
       "20170516    -0.030817   0.029412   0.036435   0.028862  -0.027542   0.018429   \n",
       "\n",
       "symbol      603858.SH  603885.SH  603993.SH  \n",
       "trade_date                                   \n",
       "20170510    -0.066559  -0.052349  -0.073826  \n",
       "20170511    -0.075232  -0.069213  -0.043578  \n",
       "20170512    -0.068654  -0.084851  -0.035129  \n",
       "20170515    -0.041775  -0.064647  -0.025641  \n",
       "20170516    -0.035728  -0.028770   0.020979  \n",
       "\n",
       "[5 rows x 330 columns]"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import cum2ret\n",
    "\n",
    "cum2ret(cum, period=1,compound=True).dropna().head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## calc_performance_metrics\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.calc_performance_metrics(ser, cum_return=False, compound=True) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 根据收益计算常见绩效——annualized return, volatility and sharpe\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|ser |是| pandas.DataFrame/pandas.Series |时间为索引的收益/累积收益表。注意：只能有一列值，不支持多列收益的计算|\n",
    "|cum_return |否| bool |收益是否为累积收益，默认为否（False）|\n",
    "|compound |否| bool |收益计算是否为复利。单利：（每期累加的收益）；复利（每期累乘的收益），默认为True 复利模式|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "绩效表\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'ann_ret': 0.055395156268065238,\n",
       " 'ann_vol': 0.4356857810045196,\n",
       " 'sharpe': 0.12714474211287285}"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import calc_performance_metrics\n",
    "\n",
    "# 多头组合的每日收益\n",
    "daily_return = calc_period_wise_weighted_signal_return(signal_data, weight_method=\"long_only\")\n",
    "# 该收益的绩效表现\n",
    "calc_performance_metrics(daily_return, cum_return=False, compound=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## period_wise_ret_to_cum\n",
    "- ` jaqs_fxdayu.research.signaldigger.performance.period_wise_ret_to_cum(ret, period, compound=True) `\n",
    "\n",
    "**简要描述：**\n",
    "\n",
    "- 从按period周期调仓的选股方案的每日收益中计算累积收益。计算方式如下：\n",
    "- 以某个调仓周期为n天的选股方案为例：将资金等分为n分，每天取其中一份买入当天的选股并持有到5天后卖出，最后的组合累积收益。\n",
    "\n",
    "**参数:**\n",
    "\n",
    "|字段|必选|类型|说明|\n",
    "|:----    |:---|:----- |-----   |\n",
    "|ret |是| pandas.DataFrame/pandas.Series |时间为索引的收益表。|\n",
    "|period |是| int |调仓周期 |\n",
    "|compound |否| bool |收益计算是否为复利。单利：（每期累加的收益）；复利（每期累乘的收益），默认为True 复利模式|\n",
    "\n",
    "**返回:**\n",
    "\n",
    "累积收益\n",
    "\n",
    "**示例：**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.066372</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.070300</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.065394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.064365</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.078423</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              return\n",
       "trade_date          \n",
       "20170503   -0.066372\n",
       "20170504   -0.070300\n",
       "20170505   -0.065394\n",
       "20170508   -0.064365\n",
       "20170509   -0.078423"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "daily_return.head() # 每一天对应的收益代表买入股票15天后卖出的收益。如20170503的return表示20170503买入股票并在20170518卖出的收益"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style>\n",
       "    .dataframe thead tr:only-child th {\n",
       "        text-align: right;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>trade_date</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>20170503</th>\n",
       "      <td>-0.004425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170504</th>\n",
       "      <td>-0.009111</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170505</th>\n",
       "      <td>-0.013471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170508</th>\n",
       "      <td>-0.017762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20170509</th>\n",
       "      <td>-0.022990</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              return\n",
       "trade_date          \n",
       "20170503   -0.004425\n",
       "20170504   -0.009111\n",
       "20170505   -0.013471\n",
       "20170508   -0.017762\n",
       "20170509   -0.022990"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from jaqs_fxdayu.research.signaldigger.performance import period_wise_ret_to_cum\n",
    "\n",
    "period_wise_ret_to_cum(daily_return, period=15, compound=True).head()"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:IIA]",
   "language": "python",
   "name": "conda-env-IIA-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
